package com.simple.framework.web.service.impl;

import com.github.zuihou.base.R;
import com.simple.business.domain.dto.sms.LoginDTO;
import com.simple.business.domain.enumeration.ThirdAuthenticationEnum;
import com.simple.business.domain.vo.login.UserLoginSuccessVO;
import com.simple.business.service.user.UserService;
import com.simple.business.service.sms.VerificationCodeService;
import com.simple.framework.security.custom.ThirdAuthenticationToken;
import com.simple.framework.web.service.ProviderAuthenticationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Service
public class PhoneAndEmailAuthenticationServiceImpl implements ProviderAuthenticationService {
    @Resource
    VerificationCodeService verificationCodeService;
    @Resource
    UserService userService;
    @Override
    public List<ThirdAuthenticationEnum> supportsProvider() {
        return Arrays.asList(ThirdAuthenticationEnum.PHONE,ThirdAuthenticationEnum.EMAIL);
    }

    @Override
    public R<UserLoginSuccessVO> authenticate(ThirdAuthenticationToken authenticationToken, HttpServletRequest request) {
        R<Boolean> accountResult = verificationCodeService.codeCheck(authenticationToken.getPrincipal().toString(), authenticationToken.getCode());
        if(accountResult.getIsError()){
            return R.fail(accountResult.getMsg());
        }
        LoginDTO loginDTO = LoginDTO.builder().type(authenticationToken.getThirdAuthenticationEnum())
                .account(authenticationToken.getPrincipal().toString())
                .build();
        return userService.loginByThirdAccount(loginDTO);
    }
}
